'use strict';

/**
 * @ngdoc directive
 * @name nodeApp.directive:page
 * @description
 * # page
 */
app.directive('page', function ($compile) {
  return {
    restrict: 'E',
    template:'<div class="page"></div>',
    replace:true,
    link: function($scope, $this, attr) {
      $scope.$on('finish-reload',function(){
        $this.html('');
        var ul = angular.element('<ul></ul>');
        var li = angular.element('<li></li>'),_li;
        //var length = Math.min($scope.maxPage-0, attr.length-0);
        var length = $scope.maxPage-0;
        ul.append(li.clone().html('&laquo;').attr('ng-click','go("up")').attr('ng-class','{disabled:page<2}'));
        for (var i = 1; i <= length; i++) {
          _li = li.clone().text(i);
          //var _li = li.clone().text($scope.page + i);
          if (i == $scope.page) _li.addClass('active');
          if(false){
            _li.addClass('disabled').text('...');
          }else{
            //_li.attr('ng-click','page='+($scope.page + i));
            _li.attr('ng-click','go('+i+')');
          }
          ul.append(_li);
        }
        ul.append(li.clone().html('&raquo;').attr('ng-click','go("down")').attr('ng-class','{disabled:page>=maxPage}'));
        $this.append(ul);
        $compile($this.contents())($scope);
      });
    }
  };
});
